﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;

namespace TreasureHunter
{
    public static class TextureLoader
    {
        private static Bitmap card;
        private static Bitmap straightVertical;
        private static Bitmap straightHorizontal;
        private static Bitmap cornerNorthEast;
        private static Bitmap cornerEastSouth;
        private static Bitmap cornerSouthWest;
        private static Bitmap cornerWestNorth;
        private static Bitmap JunctionNorthEastSouth;
        private static Bitmap JunctionEastSouthWest;
        private static Bitmap JunctionSouthWestNorth;
        private static Bitmap JunctionWestNorthEast;

        public static void LoadTextures()
        {
            Stream stream;
            System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();

            stream = myAssembly.GetManifestResourceStream("TreasureHunter.Resources.card.png");
            card = new Bitmap(stream);

            stream = myAssembly.GetManifestResourceStream("TreasureHunter.Resources.straight.png");
            straightVertical = new Bitmap(stream);

            straightHorizontal = new Bitmap(stream);
            straightHorizontal.RotateFlip(RotateFlipType.Rotate90FlipNone);


            //CORNER TEXTURES
            stream = myAssembly.GetManifestResourceStream("TreasureHunter.Resources.corner.png");
            cornerNorthEast = new Bitmap(stream);

            cornerEastSouth = new Bitmap(stream);
            cornerEastSouth.RotateFlip(RotateFlipType.Rotate90FlipNone);

            cornerSouthWest = new Bitmap(stream);
            cornerSouthWest.RotateFlip(RotateFlipType.Rotate180FlipNone);

            cornerWestNorth = new Bitmap(stream);
            cornerWestNorth.RotateFlip(RotateFlipType.Rotate270FlipNone);

            //JUNCTION TEXTURES
            stream = myAssembly.GetManifestResourceStream("TreasureHunter.Resources.junction.png");
            JunctionNorthEastSouth = new Bitmap(stream);

            JunctionEastSouthWest = new Bitmap(stream);
            JunctionEastSouthWest.RotateFlip(RotateFlipType.Rotate90FlipNone);

            JunctionSouthWestNorth = new Bitmap(stream);
            JunctionSouthWestNorth.RotateFlip(RotateFlipType.Rotate180FlipNone);

            JunctionWestNorthEast = new Bitmap(stream);
            JunctionWestNorthEast.RotateFlip(RotateFlipType.Rotate270FlipNone);
        }

        public static Bitmap GetMatchingTexture(bool isNorthOpen, bool isEastOpen, bool isSouthOpen, bool isWestOpen)
        {
            int amountOfOpenSides = CountOpenSides(isNorthOpen, isEastOpen, isSouthOpen, isWestOpen);

            if (amountOfOpenSides == 2) //2 open ends
            {
                
                if(isNorthOpen && isSouthOpen)          //North to south
                    return straightVertical;    
                else if (isEastOpen && isWestOpen)      //West to east
                    return straightHorizontal;          
                else if (isNorthOpen && isEastOpen)     //North to east |__
                    return cornerNorthEast;             //               __
                else if (isEastOpen && isSouthOpen)     //East to south |  
                    return cornerEastSouth;             //              __
                else if (isSouthOpen && isWestOpen)     //South to west   |
                    return cornerSouthWest;             //
                else if (isWestOpen && isNorthOpen)     //West to north __|
                    return cornerWestNorth;
            }
            else if (amountOfOpenSides == 3) //3 open ends
            {
                if (isNorthOpen && isEastOpen && isSouthOpen)
                    return JunctionNorthEastSouth;
                else if (isEastOpen && isSouthOpen && isWestOpen)
                    return JunctionEastSouthWest;
                else if (isSouthOpen && isWestOpen && isNorthOpen)
                    return JunctionSouthWestNorth;
                else if (isWestOpen && isNorthOpen && isEastOpen)
                    return JunctionWestNorthEast;
            }
            return card;
        }

        private static int CountOpenSides(bool North, bool East, bool South, bool West)
        {
            int countOpenSides = 0;
            if (North)
                countOpenSides++;
            if (East)
                countOpenSides++;
            if (South)
                countOpenSides++;
            if (West)
                countOpenSides++;
            return countOpenSides;
        }
    }
}
